package com.springboot.frame.excel.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.ss.usermodel.Sheet;

import java.util.Collection;
import java.util.List;

/**
 * @author madman
 */
@Data
@AllArgsConstructor
public class WriterSheet {

  /**
   * Sheet对象
   */
  private Sheet sheet;

  /**
   * 导出数据对应的Class类型
   */
  private Class<?> clazz;

  /**
   * 导出数据
   */
  private Collection<?> data;

  /**
   * 导出表标题
   */
  private String title;

  /**
   * 导出表标题行高度
   */
  private short titleHeight;

  /**
   * 导出表头行高度
   */
  private short headerHeight;

  /**
   * 导出表内容行高度
   */
  private short bodyHeight;

  /**
   * 列索引
   */
  private int rowIndex;

  /**
   * 导出数据对应的数据参数
   */
  private List<WriterParam> writerParams;

  public WriterSheet(Sheet sheet, Collection<?> data, List<WriterParam> writerParams,
      short bodyHeight, int rowIndex) {
    this.sheet = sheet;
    this.data = data;
    this.writerParams = writerParams;
    this.bodyHeight = bodyHeight;
    this.rowIndex = rowIndex;
  }

  /**
   * 列索引增加方法
   */
  public void increaseIndex() {
    rowIndex++;
  }

  /**
   * 计算表头行数
   *
   * @return 表头行数
   */
  public int calculateHeaderRowNum() {
    return calculateHeaderRowNum(writerParams);
  }

  /**
   * 计算表头行数
   *
   * @param writerParams 导出数据对应的数据参数
   * @return 表头行数
   */
  private int calculateHeaderRowNum(List<WriterParam> writerParams) {
    int num = 1;
    for (WriterParam writerParam : writerParams) {
      List<WriterParam> children = writerParam.getChildren();
      if (CollectionUtils.isNotEmpty(children)) {
        num += calculateHeaderRowNum(children);
        break;
      }
    }
    return num;
  }

}
